package main.java.com.itlin.datastructure.linkedlist;

import java.util.Iterator;
import java.util.function.Consumer;

public class SinglyLinkedListSentinel implements Iterable<Integer> {//整体

    //head 哨兵
    private Node head = new Node(666,null);//哨兵节点

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node pointer = head.next;
            @Override
            public boolean hasNext() {//询问是否有下一个元素
                return pointer != null;
            }

            @Override
            public Integer next() {//返回当前元素的值，并且指向下一个元素
                int value = pointer.value;
                pointer = pointer.next;
                return value;
            }
        };
    }

    //节点类
    private static class Node {
        int value; //值
        Node next;//下一个节点指针

        public Node(int value ,Node next){
            this.value = value;
            this.next = next;
        }
    }


    public void addFirst(int value) {
        insert(0,value);
    }

    public void loop1(Consumer<Integer> consumer){
        Node pointer =  head.next;
        while(pointer != null){
            consumer.accept(pointer.value);
            pointer = pointer.next;
        }
    }

    public void loop2(Consumer<Integer> consumer){
        for (Node pointer = head.next; pointer != null; pointer = pointer.next){
            consumer.accept(pointer.value);
        }
    }

    private Node findLast(){

        //链表非空的情况
        Node last = head;
        while(last.next != null){
            last = last.next;
        }
        return last;
    }

    public void addLast(int value){
        Node last = findLast();

        last.next = new Node(value, null);
    }

    private Node findNode(int index){
        int i = -1;
        for (Node pointer = head; pointer != null ; pointer = pointer.next, i++){
            if (i == index){
                return pointer;
            }
        }
        return null;
    }

    public int get(int index) throws IllegalArgumentException{
        Node node = findNode(index);
        if(node == null){
            throw illegalIndex(index);
        }else {
            return node.value;
        }
    }

    private static IllegalArgumentException illegalIndex(int index) {
        return new IllegalArgumentException(String.format("index[%d] 不合法%n", index));
    }

    public void insert(int index,int value){

        //非法情况
        Node previous = findNode(index - 1);//找到上一个节点

        if (previous == null){
            throw illegalIndex(index);
        }
        previous.next  = new Node(value, previous.next);

    }

    public void removeFirst(){
        remove(0);
    }

    public void remove(int index){


        Node previous = findNode(index - 1);

        if (previous == null){
            throw illegalIndex(index);
        }
        Node removed = previous.next;
        if (removed == null){
            throw illegalIndex(index);
        }
        previous.next = removed.next;
    }



}


